<?php

/**
 * Created by PhpStorm.
 * User: fanfz_000
 * Date: 2015/4/7 0007
 * Time: 19:48
 * 作用 ：
 */
class Visitinfo_model extends CI_Model
{

    var $Id = '';
    var $visit_ip = '';
    var $visit_time = '';
//    var $agent='';
//    var $agent_info='';

    function __construct()
    {
        parent::__construct();
    }
    /*设置浏览记录*/
    /*添加浏览记录记录失败，日志信息添加功能 2015-5-1*/
    function set_visit_info()
    {
        $this->load->library('user_agent');
        $where_data = array(
            'visit_time >=' => date('Y-m-d h:i:s', strtotime("-1 day")),
            'visit_ip' => $this->input->ip_address()
        );
        $tf = $this->db->select('Id')->from('visit_info')->where($where_data)->get()->row_array();
        //echo $this->db->last_query();
        $data = array(
            'visit_ip' => $this->input->ip_address(),
            'visit_time ' => date('Y-m-d h:i:s'),
//            'agent'=>"browser",
//            'agent_info'=>$this->agent->browser().' '.$this->agent->version()
        );
        if (!empty($tf)) {
            $system_blog = array(
                'text' => '用户登录信息，重复登录。访客ip为' . $data['visit_ip'] . ',访客浏览时间：' . date('Y-m-d h:i:s'),
//                'text' => '用户登录信息，重复登录。访客ip为' . $data['visit_ip'] . ',访客浏览时间：' . date('Y-m-d h:i:s').',访客设备：'.$data['agent'].',设备信息：'.$data['agent_info'],
                'date ' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
        } else {
            $bool = $this->db->insert('visit_info', $data);
            if ($bool) {
                //更新成功
                return $this->db->insert_id();
            } else {
                //数据异常
                $system_blog = array(
                    'text' => '用户登录信息，记录失败。访客ip为' . $data['visit_ip'] . ',访客浏览时间：' . $data['visit_time'] . ',sql:' . $this->db->last_query(),
                    'date' => date('Y-m-d')
                );
                $this->db->insert('system_blog', $system_blog);
            }
        }
    }

    function show_visit_info($page = 0, $limit = 10)
    {
        $query = $this->db->from('visit_info')->order_by('Id DESC')->limit($limit, $page * $limit)->get();
        $data = $query->result_array();
        return array(
            'data' => $data,
            'total' => $this->db->count_all_results('visit_info'),
            'today_total' => count($this->db->from('visit_info')->where('visit_time > date_sub(curdate(),interval 1 day)')->get()->result_array()),
        );
    }

    function show_visit_total()
    {

        for ($i = 6; $i >= 0; --$i) {
            $where_data = array(
                'visit_time >=' => date('Y-m-d H:i:s', strtotime("-" . ($i + 1) . " day")),
                'visit_time <=' => date('Y-m-d H:i:s', strtotime("-" . $i . " day")),
            );
            $array_data = $this->db->select('Id')->from('visit_info')->where($where_data)->get()->row_array();
            $return_data[6 - $i] = count($array_data);
        }
        return json_encode($return_data);
    }

}